package com.cgbl.service.ui

import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.text.TextUtils
import com.cgbl.service.BR
import com.cgbl.service.R
import com.cgbl.service.databinding.AppActivityChatBinding
import com.netease.yunxin.kit.alog.ALog
import com.netease.yunxin.kit.chatkit.ui.ChatKitClient
import com.netease.yunxin.kit.chatkit.ui.ChatKitUIConstant
import com.netease.yunxin.kit.chatkit.ui.ChatUIConfig
import com.netease.yunxin.kit.chatkit.ui.builder.P2PChatFragmentBuilder
import com.netease.yunxin.kit.chatkit.ui.page.fragment.ChatP2PFragment
import com.netease.yunxin.kit.chatkit.ui.view.message.MessageProperties
import com.netease.yunxin.kit.corekit.im.model.UserInfo
import com.netease.yunxin.kit.corekit.im.utils.RouterConstant.CHAT_ID_KRY
import com.netease.yunxin.kit.corekit.im.utils.RouterConstant.CHAT_KRY
import com.robot.base.base.activity.GLBaseActivity
import com.robot.base.base.vm.GLBaseViewModel


class ChatActivity : GLBaseActivity<AppActivityChatBinding, GLBaseViewModel>() {

    private val TAG = "ChatActivity"

    private lateinit var chatFragment: ChatP2PFragment

    override fun initContentView(savedInstanceState: Bundle?): Int = R.layout.app_activity_chat

    override fun initVariableId(): Int = BR.viewModel

    override fun initComponents() {
        val userInfo = intent.getSerializableExtra(CHAT_KRY) as UserInfo?
        val accId = intent.getStringExtra(CHAT_ID_KRY) ?: ""
        ALog.e(
            ChatKitUIConstant.LIB_TAG, TAG, "initChat:$accId"
        )
        if (userInfo == null && TextUtils.isEmpty(accId)) {
            ALog.e(
                ChatKitUIConstant.LIB_TAG, TAG, "user info is null && accid is null:"
            )
            finish()
            return
        }
        val chatUIConfig = ChatUIConfig()
        val messageProperties = MessageProperties()
        messageProperties.chatViewBackground =
            ColorDrawable(resources.getColor(R.color.app_F7F9FA));
        chatUIConfig.messageProperties = messageProperties
        ChatKitClient.setChatUIConfig(chatUIConfig)
        //set fragment
        //set fragment
        val fragmentBuilder = P2PChatFragmentBuilder()

        chatFragment = fragmentBuilder.build()
        val bundle = Bundle()
        bundle.putSerializable(CHAT_ID_KRY, accId)
        bundle.putSerializable(CHAT_KRY, userInfo)
        chatFragment.arguments = bundle

        val fragmentManager = supportFragmentManager
        fragmentManager.beginTransaction().add(R.id.chat_container, chatFragment)
            .commitAllowingStateLoss()
    }
}